let SWDA_BattleUIPatch =  {};





//=========================================================
// BattleLog文字改白色
//=========================================================
// 战斗帮助窗口位置
SWDA_BattleUIPatch.Scene_Battle_CreateAllWindows = Scene_Battle.prototype.createAllWindows;
Scene_Battle.prototype.createAllWindows = function () {
    SWDA_BattleUIPatch.Scene_Battle_CreateAllWindows.call(this);
    this._helpWindow.y = Graphics.boxHeight - this._helpWindow.height;
};

// 把addwindow改成addchilde，窗口可透明叠加，不会刷掉底层窗口
Scene_Battle.prototype.createHelpWindow = function() {
    this._helpWindow = new Window_Help();
    this._helpWindow.visible = false;
    this.addChild(this._helpWindow);
};
//=========================================================
// BattleLog文字改白色
//=========================================================
SWDA_BattleUIPatch.WindowBase_resetFontSettings =
	Window_Base.prototype.resetFontSettings;
Window_BattleLog.prototype.resetFontSettings = function() {
	SWDA_BattleUIPatch.WindowBase_resetFontSettings.call(this)
	this.changeTextColor(this.textColor(0));
};



//=========================================================
// 状态动画位置（YEP_X_ActSeqPack2.js）
//=========================================================
SWDA_BattleUIPatch.Sprite_Battler_UpdateStateSprites = Sprite_Battler.prototype.updateStateSprites;
Sprite_Battler.prototype.updateStateSprites = function() {
	SWDA_BattleUIPatch.Sprite_Battler_UpdateStateSprites.call(this);
	if (this._stateSprite){
		this._stateSprite.y += 100;
	}
};

//=========================================================
// BattleLog
//=========================================================
Window_BattleLog.prototype.initialize = function() {
    var width = this.windowWidth();
    var height = this.windowHeight();
	var x = (Graphics.boxWidth - width) / 2;
    Window_Selectable.prototype.initialize.call(this, x, 0, width, height);
    this.opacity = 0;
    this._lines = [];
    this._methods = [];
    this._waitCount = 0;
    this._waitMode = '';
    this._baseLineStack = [];
    this._spriteset = null;
    this.createBackBitmap();
    this.createBackSprite();
    this.refresh();
};

Window_BattleLog.prototype.windowWidth = function() {
    return Graphics.boxWidth;
};

// 覆写，统一居中
Window_BattleLog.prototype.drawLineText = function(index) {
    if (this._lines[index].match('<SIMPLE>')) {
      this.drawSimpleActionLine(index);
    } else {
      //Yanfly.BEC.Window_BattleLog_drawLineText.call(this, index);
	  this.drawCenterLine(index);
    }
};


Window_BattleLog.prototype.drawSimpleActionLine = function(index) {
    var text = this._lines[index].replace('<SIMPLE>', '');
    text = text.split("（")[0] + (!!text.split("）")[1] ? text.split("）")[1] : "");
    var rect = this.itemRectForText(index);
    this.contents.clearRect(rect.x, rect.y, rect.width, rect.height);
    if (this._actionIcon) {
      var tw = this.textWidth(text);
      var ix = (rect.width - tw - Window_Base._iconWidth) / 2;
      this.drawIcon(this._actionIcon, ix, rect.y + 2);
    }
    this.drawText(text, rect.x + Window_Base._iconWidth / 2, rect.y, rect.width, 'center');
};
// 显示时长
Window_BattleLog.prototype.messageSpeed = function() {
    return 32;
};

Window_BattleLog.prototype.wait = function() {
    this._waitCount = this.messageSpeed();

};

// 变更等待时间
Window_BattleLog.prototype.waitForNewLine = function() {
    var baseLine = 0;
    if (this._baseLineStack.length > 0) {
        baseLine = this._baseLineStack[this._baseLineStack.length - 1];
    }
    /*
    if (this._lines.length > baseLine + 1) { 
        this.wait();
    }*/
    // 去除最后一项多余的等待
    if (this._lines.length > baseLine ){
        this._waitCount = 16;
    }

    // 法宝额外显示时间
    if (this._lines.length > baseLine + 1) { 
        if(this._lines[this._lines.length - 1].contains('法宝')){
            this._waitCount += 50;
        }
    }
};
//=========================================================
// 敌人信息
//=========================================================

// 敌人编号（改中文）
Game_Troop.LETTER_TABLE_FULL = [
    ' 一',' 二',' 三',' 四',' 五',' 六',' 七',' 八',' 九',' 十',
	' 十一',' 十二',' 十三',' 十四',' 十五',' 十六',' 十七', ' 十八',' 十九',' 二十',
	' 二十一',' 二十二',' 二十三',' 二十四',' 二十五',' 二十六'
];

// 血条文字颜色（YEP_X_VisualHpGauge.js）
Window_VisualHPGauge.prototype.hpColor = function(actor) {
    if (actor.isDead()) {
        return this.deathColor();
    } else if (actor.isDying()) {
        return this.crisisColor();
    } else {
        return this.textColor(0);
    }
};
// 绘制角色名（YEP战斗核心）
if (Imported.YEP_BattleEngineCore){
	Window_EnemyVisualSelect.prototype.normalColor = function() {
		return this.textColor(0);
	};
	
	Window_EnemyVisualSelect.prototype.resetFontSettings = function() {
		this.setSmallerFont();
		//this.contents.fontFace = this.standardFontFace();
		this.contents.fontSize = Yanfly.Param.BECEnemyFontSize;
		this.resetTextColor();
	};
	Window_EnemyVisualSelect.prototype.refresh = function() {
		this.contents.clear();
		if (!this._battler) return;
		if (!this._showEnemyName) return;
		if (this._battler.isHidden()) return;
        if (!this._battler.battler()._visualHpGauge) return;
        if (!this._battler.battler()._visualHpGauge.isShowWindow()) return;
		this._requestRefresh = false;
		var wy = 0;
		var wx = 0;
		this.changeTextColor(this.textColor(0));
		this.drawTextEx(this.enemyClassText(this._battler._classId)[0], wx, wy);
		this.drawText(this._battler.name().split('（')[0], wx + 34, wy, this.contents.width);
	};
}

SWDA_BattleUIPatch.drawCurrentAndMax = Window_Base.prototype.drawCurrentAndMax;
Window_VisualHPGauge.prototype.drawCurrentAndMax = function (current, max, x, y,
    width, color1, color2) {
		this.setSmallerFont();
		this.contents.fontSize = 20;
		SWDA_BattleUIPatch.drawCurrentAndMax.call(this, current, max,
        x, y, width, color1, color2);
    this.resetFontSettings();
}

// 名称位置（HP左侧）
Window_EnemyVisualSelect.prototype.updateWindowSize = function() {
    this.contents.fontSize = Yanfly.Param.BECEnemyFontSize;
    if (this._nameTextWidth === undefined) {
      this._nameTextWidth = this.textWidth(this._battler.name().split('（')[0]);
    }
    var textWidth = this._nameTextWidth;
    textWidth += this.textPadding() * 2;
    var width = textWidth + this.standardPadding() * 2 + Window_Base._iconWidth;
    width = Math.ceil(width);
    var height = this.lineHeight() + this.standardPadding() * 2;
    if (width === this.width && height === this.height) return;
    this.width = width;
    this.height = height;
    this.createContents();
    this._requestRefresh = true;
    this.makeWindowBoundaries();
};

Window_EnemyVisualSelect.prototype.updateWindowPosition = function() {
    if (!this._battler) return;
	var battler = this._battler;

    this.x = battler.spritePosX() - Math.ceil(this.width / 2);
    this.y = battler.spritePosY() - Math.floor((battler.spriteHeight() + battler.floatHeight()*battler.isFloating()) * battler.battler().perspectiveScale());
    this.y -= this.standardPadding() * 2;

    if (!!battler.battler()._visualHpGauge){
        if (battler.battler()._visualHpGauge.isShowWindow()){
            this.x = battler.battler()._visualHpGauge.x - this.width + this.standardPadding() * 2;
            this.y = battler.battler()._visualHpGauge.y;
        }
    }

    this.x = this.x.clamp(this._minX, this._maxX);
    this.y = this.y.clamp(this._minY, this._maxY);
    if(!!BattleManager._petWindowA)
        if( BattleManager._petWindowA.isOpen() && this.x < 195)
            this.y = this.y.clamp(this._minY + 36,  this._maxY)
    if(!!BattleManager._petWindowB)
        if(BattleManager._petWindowB.isOpen() && this.x < 390)
            this.y = this.y.clamp(this._minY + 36,  this._maxY)
};

Window_EnemyVisualSelect.prototype.makeWindowBoundaries = function() {
    if (!this._requestRefresh) return;
    this._minX = -1 * this.standardPadding();
    this._maxX = Graphics.boxWidth - this.width + this.standardPadding();
    this._minY = this.standardPadding();
    this._maxY = Graphics.boxHeight - this.height + this.standardPadding();
    //this._maxY -= SceneManager._scene._statusWindow.height;   //不根据状态窗口限制坐标
};

//////////////////////////////////////////////////////////////////////////////////////////////////////
// 敌人状态图标位置
SWDA_BattleUIPatch.Sprite_Enemy_UpdateStateSprite = Sprite_Enemy.prototype.updateStateSprite;
Sprite_Enemy.prototype.updateStateSprite = function() {
	SWDA_BattleUIPatch.Sprite_Enemy_UpdateStateSprite.call(this);
	/*
	if  ( (this.y + this._stateIconSprite.y) * this.scale.y >= 120) {
		this._stateIconSprite.y =  Math.ceil( (-this.bitmap.height + Window_Base._iconWidth - 16)/this.scale.y);
	} else {
		this._stateIconSprite.y =  Math.ceil( (-this.bitmap.height + Window_Base._iconWidth - 16)/this.scale.y + 120 - 2 * 32);
	}
	this._stateIconSprite.x = -this._battler.hpGaugeWidth()/2 + this._stateIconSprite.width/2;
	*/
	if (this._visualHpGauge){
		this._stateIconSprite.y = Math.floor((this._visualHpGauge.y - this.y + 32*2 + 20) / this._enemy.battler().perspectiveScale());
		//this._stateIconSprite.x = Math.floor((this._visualHpGauge.x + this.width - this.x + 32 ) /this._enemy.battler().perspectiveScale());
        this._stateIconSprite.x = Math.floor((this.width/2 * 0.8- 32 ).clamp(0,250) /this._enemy.battler().perspectiveScale());
        
        this.stateBalloonSprite.x = this._stateIconSprite.x;
        this.stateBalloonSprite.y = this._stateIconSprite.y;
        //this.stateBalloonSprite.visible = this._stateIconSprite._iconIndex === 0 ? false : true;
        if (this._stateIconSprite._iconIndex === 0){
            if (this.stateBalloonSprite.opacity > 0)
                this.stateBalloonSprite.opacity -= 16;
                this._stateIconSprite.opacity -= 16;
            
        }else{
            this.stateBalloonSprite.visible = true
            if (this.stateBalloonSprite.opacity < 255)
                this.stateBalloonSprite.opacity += 16;
                this._stateIconSprite.opacity += 16;
        }

        if (this.stateBalloonSprite.opacity < 0){
            this.stateBalloonSprite.opacity = 16;
            this._stateIconSprite.opacity = 16;
        }else if (this.stateBalloonSprite.opacity >255){
            this.stateBalloonSprite.opacity = 255;
            this._stateIconSprite.opacity = 255;
        }

        const rate = this.stateBalloonSprite.opacity / 255;
        this._stateIconSprite.scale.x = rate / this._enemy.battler().perspectiveScale();
        this._stateIconSprite.scale.y = rate / this._enemy.battler().perspectiveScale();
        this.stateBalloonSprite.scale.x = rate / this._enemy.battler().perspectiveScale();
        this.stateBalloonSprite.scale.y = rate / this._enemy.battler().perspectiveScale();
	}



    
};
// 敌人状态图标气泡背景
Sprite_Enemy.prototype.createStateIconSprite = function() {
    this.stateBalloonSprite = new Sprite();
    this.stateBalloonSprite.bitmap = ImageManager.loadBitmap('img/system/UI/', 'stateBalloon');
    this.stateBalloonSprite.anchor.x = 0.5;
    this.stateBalloonSprite.anchor.y = 0.4;
    this.stateBalloonSprite.visible = true;
    this.stateBalloonSprite.opacity = 0;
    this.addChild(this.stateBalloonSprite);

    this._stateIconSprite = new Sprite_StateIcon();
    this.addChild(this._stateIconSprite);
};

// 开菜单时隐藏祭符图标（关联Window_BattleSkill和Window_BattleItem窗口的show和hide（需要request刷新）
Sprite_GlobalElement.prototype.isElementVisibility = function(){
	return this.elementCount() > 0 && !SceneManager._scene._skillWindow.active && !SceneManager._scene._itemWindow.active;
};